/*****************************************************************************/
/**
*
* @file esp8266.c
*
* User defined functions about esp8266 control.
* Target device: STM32F103RET6.
* Target board: ONENET kylin v2.3
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver   Who    Date   	Changes
* ----- ---- ---------- -------------------------------------------------------
* 0.01  abu  02/19/2020 Created
* </pre>
*
*------------------------------------------------------------------------------
* Author:	abu
*					E-mail:	abu_liu@opencores.org
*------------------------------------------------------------------------------
*
******************************************************************************/

/***************************** Include Files *********************************/
#include "esp8266.h"

ESP8266_RECV ESP8266_Recv_Info;


/************************** Function Prototypes ******************************/

/*****************************************************************************/
/**
*
* @brief	Initialise esp8266.
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void ESP8266_Init(void)
{
	//====================gpio related, resrt signal========================
	GPIO_InitTypeDef ESP8266_RST_GPIO_Init_Struct;
	
	//open gpio port clock
	RCC_APB2PeriphClockCmd(ESP8266_RST_GPIO_PERIPH_PORT, ENABLE);
	
	//reset gpio pin, PA9
	ESP8266_RST_GPIO_Init_Struct.GPIO_Pin = ESP8266_RST_GPIO_PIN;
	ESP8266_RST_GPIO_Init_Struct.GPIO_Speed = GPIO_Speed_50MHz;
	ESP8266_RST_GPIO_Init_Struct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(ESP8266_RST_GPIO_PORT, &ESP8266_RST_GPIO_Init_Struct);

	ESP8266_DeviceOn();
	
	//=========================usart related=================================
	USART2_Init(ESP8266_USART_BAUD);

	//clear receive buffer
	ESP8266_Recv_Info.dataLen = 0;
	
	//AT command, set esp8266
	ESP8266_WriteCmd("AT+RST\r\n");
	delay(5000);
	ESP8266_WriteCmd("AT+CWMODE=1\r\n");
	delay(5000);
	ESP8266_WriteCmd("AT+CWJAP=\"TP-LINK_89D4\",\"ZhangLiu@1112\"\r\n");
	delay(10000);
	ESP8266_WriteCmd("AT+CIPSTATUS\r\n");
}

/*****************************************************************************/
/**
*
* @brief	Reset esp8266.
*
* @param	None.
* @param  None.	
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
void ESP8266_Rst(void)
{
	ESP8266_DeviceRst();
	Delay(200);
	ESP8266_DeviceRst();
	Delay(200);
}